{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "from local_plot import *\n",
    "from utils import *\n",
    "import re\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_broadcast_D2VINS_from_log(log_content):\n",
    "    # Sample log:\n",
    "    # [D2VINS] Broadcast VINS Data size 4144 with 54 poses 10 extrinsic.\n",
    "    # Match using regex, to match broadcast poses and extrinsic\n",
    "    pattern = re.compile(r\"\\[D2VINS\\] Broadcast VINS Data size (\\d+) with (\\d+) poses (\\d+) extrinsic.\")\n",
    "    matched = pattern.findall(log_content)\n",
    "    # Find the last line of log_content contain broadcast poses\n",
    "    lines = log_content.splitlines()\n",
    "    c = len(lines)\n",
    "    for line in reversed(lines):\n",
    "        matched = pattern.findall(line)\n",
    "        c -= 1\n",
    "        if matched:\n",
    "            break\n",
    "    matched = np.array(matched, dtype=float)\n",
    "    return np.sum(matched[:,0]), np.sum(matched[:, 1]) + np.sum(matched[:, 2]), c\n",
    "\n",
    "def extract_broadcast_D2PGO_from_log(log_content):\n",
    "    # [Drone 2] DPGO broadcast poses 249\n",
    "    # Match using regex, to match broadcast poses\n",
    "    pattern = re.compile(r\"\\[Drone \\d+\\] DPGO broadcast poses (\\d+)\")\n",
    "    matched = pattern.findall(log_content)\n",
    "    matched = np.array(matched, dtype=float)\n",
    "    return np.sum(matched)\n",
    "\n",
    "def extract_broadcast_poses(log_content):\n",
    "    broadcast_D2VINS, sum_poses, line_no = extract_broadcast_D2VINS_from_log(log_content)\n",
    "    broadcast_D2PGO = extract_broadcast_D2PGO_from_log(log_content)\n",
    "    return broadcast_D2VINS + broadcast_D2PGO*2, sum_poses\n",
    "\n",
    "def extract_D2Comm_broadcast_size(log_content):\n",
    "    #[D2Comm] Broadcast PGO data of drone 1, lcm 27576 bytes. \n",
    "    # Match using regex, to match broadcast poses\n",
    "    pattern = re.compile(r\"\\[D2Comm\\] Broadcast PGO data of drone \\d+, lcm (\\d+) bytes.\")\n",
    "    matched = pattern.findall(log_content)\n",
    "    matched = np.array(matched, dtype=float)\n",
    "    return np.sum(matched)\n",
    "\n",
    "def extract_frontend_sum_kb(log_content):\n",
    "    # [SWARM_LOOP](5584) BD KF 1005889@1 LM: 76 size 10211 header 311 avgsize 20120 sumkB 109713 avgLM 137 need_send_features: 1\n",
    "    # Match using regex, to match last sumkB\n",
    "    pattern = re.compile(r\"\\[SWARM_LOOP\\]\\(\\d+\\) BD KF \\d+@\\d+ LM: \\d+ size \\d+ header \\d+ avgsize \\d+ sumkB (\\d+) avgLM \\d+ need_send_features: \\d+\")\n",
    "    matched = pattern.findall(log_content)\n",
    "    matched = np.array(matched, dtype=float)\n",
    "    return matched[-1]\n",
    "    \n",
    "    \n",
    "def process_bd_for_backend(path, nodes=range(1, 6)):\n",
    "    broadcast_poses = 0\n",
    "    frontend_sum_kb = 0\n",
    "    d2vins_size = 0\n",
    "    d2pgo_size = 0\n",
    "    for i in nodes:\n",
    "        log_vo = f\"{path}{i}/d2slam.log\"\n",
    "        with open(log_vo, \"r\") as f:\n",
    "            log_content = f.read()\n",
    "            _d2vins_size, _broadcast_poses = extract_broadcast_poses(log_content)\n",
    "            d2vins_size += _d2vins_size\n",
    "            broadcast_poses += _broadcast_poses\n",
    "            frontend_sum_kb += extract_frontend_sum_kb(log_content)\n",
    "            d2pgo_size += extract_D2Comm_broadcast_size(log_content)\n",
    "\n",
    "    print(f\"Total broadcast poses {broadcast_poses/1024/1024:.1f}\")\n",
    "    print(f\"Total frontend sum {frontend_sum_kb/1024:.1f} MB\")\n",
    "    print(f\"Total d2vins size {d2vins_size/1024/1024:.1f} MB\")\n",
    "    print(f\"Total d2pg size {d2pgo_size/1024/1024:.1f} MB\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/tum_datasets/outputs/d2slam/swarm\"\n",
    "process_bd_for_backend(path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/quadcam_7inch_n3_2023_1_14/outputs/d2slam-5-yaw/swarm\"\n",
    "process_bd_for_backend(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/quadcam_7inch_n3_2023_1_14/outputs/d2slam-5-yaw-compact/swarm\"\n",
    "process_bd_for_backend(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/tum_corr/outputs/d2slam/swarm\"\n",
    "process_bd_for_backend(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/tum_datasets/outputs/d2slam/swarm\"\n",
    "process_bd_for_backend(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/tum_datasets/outputs/d2slam-compact/swarm\"\n",
    "process_bd_for_backend(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/tum_corr/outputs/d2slam_compact/swarm\"\n",
    "process_bd_for_backend(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/ri_realsense_walkaround_2022_10/outputs/fuse_all-3/swarm\"\n",
    "process_bd_for_backend(path, [1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"/home/xuhao/data/d2slam/ri_realsense_walkaround_2022_10/outputs/d2slam_compact/swarm\"\n",
    "process_bd_for_backend(path, [1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "08ce52785f0fedc81003ce387e097a83d6cc9494681cd746006386992005bb71"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
